package com.dengyafei.service.impl;

import com.auth0.jwt.JWT;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.RedisKeyConstant;
import com.dengyafei.domin.vo.AdminLoginVO;
import com.dengyafei.service.AdminService;
import com.dengyafei.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 邓亚非
 */
@Service
public class AdminLoginServiceImpl implements AdminService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    /**
     * 管理员快捷登录
     * @param adminLoginVO
     * @return
     */
    @Override
    public String adminLogin(AdminLoginVO adminLoginVO) {
        Boolean aBoolean = stringRedisTemplate.hasKey(adminLoginVO.getUsername());
        Map<String,String> map=new HashMap<>();
        if (aBoolean.equals(false)){
            stringRedisTemplate.opsForValue().set(RedisKeyConstant.AdminKeyRedis.ADMIN_LOGIN_KEY,adminLoginVO.getPassword());
            map.put("userId",adminLoginVO.getUsername());
            return JwtUtil.generateToken(map);
        }
            String s = stringRedisTemplate.opsForValue().get(RedisKeyConstant.AdminKeyRedis.ADMIN_LOGIN_KEY);
            if (!s.equals(adminLoginVO.getPassword())){
                return RecommendConstant.ADMIN_LOGIN_FAILURE;
            }
            map.put("userId",adminLoginVO.getUsername());
        return JwtUtil.generateToken(map);
    }
}
